var app = getApp();
var base64 = require('../../we7/resource/js/base64.js');
Page( Object.assign({},{
    data:{
        info:'',
        allgoods : [],
        allvideo : [],
        allwork : [],
        color : '',
        tabindex : 'v',
        freetime : 0,
        vkey:-1,
        isplaying : false,
        video : false,
        vurl : false,
        ispause : false,
        gid : 0,
        vtype : 0,
        currentper : 0,
        audiopause : false,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },

    onLoad:function(options){
        var self = this;
        var id = options.id ? options.id : '';
        var gid = options.gid > 0 ? options.gid : 0;

        self.setData({
            options : options,
            gid : gid,
        })
        app.com.auth(function(user){
            app.com.http('study','POST',{op:'info',id:id,gid:gid},0,false,'','',function(res){
                if (!res.data.errno && res.data) {

                    var color = app.com.mainColor(res.data.data.params.style);

                    self.setData({
                        info : res.data.data.info,
                        allgoods : res.data.data.allgoods,
                        allvideo : res.data.data.allvideo,
                        allwork : res.data.data.allwork,
                        color : color,
                        params : res.data.data.params,
                    },function(){
                        self.setData({
                            inited : true,
                        });
                    });

                    if( res.data.data.content ){
                        var octoparse = require('../resource/wxParse/html2json2.js');
                        var transData = octoparse.parse(res.data.data.content);                   
                        self.setData({
                            nodes : transData,
                        })
                    }

                    wx.setNavigationBarTitle({
                        title: res.data.data.info.name
                    });

                }else{
                    app.com.alert(res.data.message,function(){
                        app.com.backPage(0);
                    });
                }
            });
        })
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });
        wx.stopPullDownRefresh();
    },
    onChange(e){
        this.setData({
            tabindex : e.detail.name
        })
    },
    playtop(){
        this.setData({
            currentper : 0,
        })
        if(this.data.vurl){
            this.playv();
        }else{
            var e = {
                currentTarget : {
                    dataset : {
                        id : this.data.allvideo[0].id,
                        vkey : 0,
                    }
                }
            }
            this.play(e);
        }
    },
    pauseaudio(){
        if(this.data.audiopause){
            this.data.video.play();
        }else{
            this.data.video.pause();
        }
        this.setData({
            audiopause : !this.data.audiopause
        })
    },
    play(e){
        var self = this;
        var postdata = {
            id:e.currentTarget.dataset.id,
            op:'play',
        };
        self.stopv();
        app.com.http('study','POST',postdata,0,true,'','',function(res){
            if (res.data && !res.data.errno) {
                
                if( res.data.data.key ){
                    self.setData({
                        vurl : base64.base64_decode(res.data.data.key),
                        vkey : e.currentTarget.dataset.vkey,
                        vtype : res.data.data.vtype,
                        currentper : 0,
                        freetime : res.data.data.freetime > 0 ? res.data.data.freetime : 0,
                    })
                    self.playv();
                }

            }else{
                app.com.alert(res.data.message,function(){
                    if( res.data.errno == 2 ){
                        self.setData({
                            tabindex : 1
                        })
                        var tab = self.selectComponent("#tab");
                        tab.setCurrentIndex(1);
                    }
                });
            }
        });
    },
    playv(){
        var self = this;
        if( this.data.ispause ){
            this.data.video.play();
        }else{
            if( this.data.video ){
                this.data.video.stop();
                this.data.video = false;
            }
            if(this.data.vtype == 0){
                this.data.video = wx.createVideoContext('myVideo');
            }else{
                this.data.video = wx.createInnerAudioContext();
                this.data.video.src = this.data.vurl;
                this.data.video.onTimeUpdate(function(rs){
                    var cur = self.data.video.currentTime;
                    let per = cur*100/self.data.video.duration;

                    if( per-1 >= self.data.currentper ){
                        self.setData({
                            currentper : parseInt(per),
                        })
                    }

                    var e = {
                        detail : {
                            currentTime : cur
                        }
                    }
                    self.playtime(e);

                });
                this.data.video.onEnded(() => {
                    self.stopv();
                });
            }
            
            this.data.video.seek(0);
            this.data.video.play();
        }
        this.setData({
            isplaying : true,
            ispause : false,
        })
    },
    stopv(){
        this.setData({
            isplaying : false,
            ispause : false,
            currentper : 0,
        })
    },
    pausev(){
        this.setData({
            ispause : true,
            isplaying : false,
        })
    },
    playtime(e){

        if( this.data.isdeal ) return false;
        if( this.data.freetime > 0 && e.detail.currentTime >= this.data.freetime ){
            if( this.data.isdeal ) return false;
            this.data.isdeal = true;
            this.data.video.stop();
            this.data.video.seek(0);
            app.com.alert('购买后才可播放后续内容');
            this.data.isdeal = false;
            this.setData({
                isplaying : false,
            })
        }
        
    },
    tobuy(){
        if(this.data.allgoods.length >= 2 && this.data.params.studyggg == 1 ){
            this.setData({
                tabindex : 1
            })
            var tab = this.selectComponent("#tab");
            tab.setCurrentIndex(1);
        }else{
            wx.navigateTo({
                url : this.data.allgoods[0].url
            })
        }
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e);
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    FormIdsubmit : function(e){

    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    FormIdsubmit : function(e){
        if( app.globalData.formarr.length <= 20 ) app.globalData.formarr.push(e.detail.formId);
    },
    
}))